package com.mebs.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;
import org.jt.jet.content.JetNamedObject;

import javax.persistence.*;

/**
 * Class Role
 * User: sar
 */
@Entity()
@Table(name = "ROLE")
public class Role implements JetNamedObject {

    public static final String NAME_CLIENT_MANAGER = "ClientManager";
    public static final String NAME_CLIENT_SHIPPING_CSR = "ClientShipping";
    public static final String NAME_CLIENT_SERVICE_CSR = "ClientService";
    public static final String NAME_CLIENT_HHG_USER = "HHG User";
    public static final String NAME_MEBS_ADMIN = "MEBSAdmin";
    public static final String NAME_MEBS_MANAGER = "MEBSManager";
    public static final String NAME_MEBS_SHIPPING_CSR = "MebsShipping";
    public static final String NAME_MEBS_SERVICE_CSR = "MebsService";

    public static final String [] ClientReadOnly  = new String [] {"ClientManager","ClientShipping", "ClientService"};
    public static final String [] UsersReadOnly  = new String [] {"ClientManager","ClientShipping", "ClientService", "HHG User","MEBSManager", "MebsShipping", "MebsService" };


    public static final String [] ClientReadAccess  = new String []{"ClientManager","ClientShipping", "ClientService", "MEBSAdmin", "MEBSManager", "MebsShipping" };
    public static final String [] UsersReadAccess  = new String []{"ClientManager", "MEBSAdmin", "MEBSManager" };
    public static final String [] ReportsReadAccess  = new String []{"ClientManager","ClientShipping", "ClientService", "MEBSAdmin", "MEBSManager", "MebsShipping" };
    public static final String [] ServicesReadAccess  = new String []{ "ClientService",  "MEBSAdmin", "MEBSManager"};
    public static final String [] LogisticsReadAccess = new String []{"ClientShipping", "MEBSAdmin", "MEBSManager", "MebsShipping" };

    public static final String [] ClientFullAccess  = new String [] { "MEBSAdmin", "MEBSManager"};
    public static final String [] UsersFullAccess  = new String [] { "MEBSAdmin", "MEBSManager"};
    public static final String [] ReportsFullAccess = new String []{"ClientManager","MEBSAdmin", "MEBSManager", "MebsShipping" };
    public static final String [] LogisticsFullAccess = new String []{"MEBSAdmin", "MEBSManager", "MebsShipping" };
    public static final String [] AgentsFullAccess  = new String [] { "MEBSAdmin", "MEBSManager", "MebsShipping" };
    //{"ClientManager","ClientShipping", "ClientService", "HHG User", "MEBSAdmin", "MEBSManager", "MebsShipping", "MebsService" };

    public static final String [] ServicesAccess  = new String [] { "ClientService", "MEBSAdmin", "MEBSManager" };
    public static final String [] AgentsAccess  = new String [] { "MEBSAdmin", "MEBSManager", "MebsShipping" };
    public static final String [] UsersAccess  = new String [] {"ClientManager", "MEBSAdmin", "MEBSManager"  };
    public static final String [] CompaniesAccess  = new String []{"ClientManager","ClientShipping", "ClientService",  "MEBSAdmin", "MEBSManager", "MebsShipping"};


//    public static final String [] ServicesNoAccess  = new String [] { "ClientManager","ClientShipping", "HHG User", "MebsShipping", "MebsService"  };
//    public static final String [] AgentsNoAccess  = new String [] { "ClientManager","ClientShipping", "ClientService", "HHG User", "MebsService"  };
//    public static final String [] UsersNoAccess  = new String [] { "MebsShipping", "HHG User", "MebsService" };
//    public static final String [] CompaniesNoAccess  = new String [] { "MebsShipping", "HHG User", "MebsService" };


    public static final Map<String, String []> tabAccess = new  HashMap<String, String []>();

    // stop-list for showing tabs
    static {

        tabAccess.put("Services", ServicesAccess);
        tabAccess.put("Agents", AgentsAccess);
        tabAccess.put("Users", UsersAccess);
        tabAccess.put("Companies", CompaniesAccess);
        tabAccess.put("Company", CompaniesAccess);

//        tabAccess.put("Services", ServicesNoAccess);
//        tabAccess.put("Agents", AgentsNoAccess);
//        tabAccess.put("Users", UsersNoAccess);
//        tabAccess.put("Companies", CompaniesNoAccess);
//        tabAccess.put("Company", CompaniesNoAccess);
    }
    /*
    public static final String NAME_CREATOR = "Creator";
    public static final String NAME_OWNER = "Owner";
    public static final String NAME_ANY = "Any";
     */

    public enum Name {

        CLIENT_MANAGER(1, NAME_CLIENT_MANAGER, "Client Manager", false),
        CLIENT_SHIPPING_CSR(2, NAME_CLIENT_SHIPPING_CSR, "Client Shipping CSR", false),
        CLIENT_PERSONNEL_CSR(3, NAME_CLIENT_SERVICE_CSR, "Client Service CSR", false),
        MEBS_ADMIN(4, NAME_MEBS_ADMIN, "MEBS Admin", true),
        MEBS_MANAGER(5, NAME_MEBS_MANAGER, "MEBS Manager", true),
        MEBS_SHIPPING_CSR(6, NAME_MEBS_SHIPPING_CSR, "MEBS Shipping CSR", true),
        MEBS_PERSONNEL_CSR(7, NAME_MEBS_SERVICE_CSR, "MEBS Service CSR", true),
        CLIENT_HHG_USER(8, NAME_CLIENT_HHG_USER, "HHG User", true);

        /*
        CREATOR(0, NAME_CREATOR, "Entity creator", false),
        OWNER(0, NAME_OWNER, "Entity owner", false),
        ANY(0, NAME_ANY, "Any role", false);
         */
        public final long id;
        public final String name;
        public final String description;
        public final boolean projectRole;

        Name(long id, String name, String description, boolean projectRole) {
            this.id = id;
            this.name = name;
            this.description = description;
            this.projectRole = projectRole;
        }

        public boolean isLogical() {
            return this.id == 0;
        }
    }

    public enum CommonReadOnly {
        CLIENT_MANAGER(1),
        CLIENT_SHIPPING_CSR(2),
        CLIENT_PERSONNEL_CSR(3),
        MEBS_ADMIN(4),
        MEBS_MANAGER(5),
        MEBS_SHIPPING_CSR(6);
         
       public final long id;

        CommonReadOnly(long id) {
            this.id = id;
        }

        public boolean equals(Role role){
            return this.id == role.id;
        }
    }

    public enum CommonReadWrite {
        MEBS_ADMIN(4);
        public final long id;

        CommonReadWrite(long id) {
            this.id = id;

        }
    }

    @Transient()
    public boolean hasCommonReadOnlyAccess() {
         for(CommonReadOnly cro : CommonReadOnly.values()){
             if (cro.equals(this)){
                 return true;
             }
         }
         return false;
    }


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;
    @Column(name = "Name", length = 64, nullable = false)
    private String name;
    @Column(name = "Description", length = 1024)
    private String description;
    /**
     * MEBS role flag.
     * MEBS roles can be granted only to MEBS personnel.
     */
    @Column(name = "MebsRole", nullable = false)
    private Boolean mebsRole;

    public Role() {
    }

    public Role(Long id, String name, String description, Boolean projectRole) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.mebsRole = projectRole;
    }

    /**
     * **********************
     * Getters-setters
     * ***********************
     */
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Boolean isMebsRole() {
        return mebsRole;
    }

    public void setMebsRole(Boolean mebsRole) {
        this.mebsRole = mebsRole;
    }

    @Override
    public int hashCode() {
        if (id == null) {
            return super.hashCode();
        }
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (this == o) {
            return true;
        }
        if (id == null) {
            return false;
        }
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o)) {
            return false;
        }
        final Role that = (Role) o;
        return id.equals(that.getId());
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Transient
    public Object getObjectId() {
        return id;
    }

    @Transient
    public String getObjectName() {
        return description;
    }
}
